package com.yserp.project.erp.order.saleorder.controller;

import com.yserp.common.utils.poi.ExcelUtil;
import com.yserp.framework.aspectj.lang.annotation.Log;
import com.yserp.framework.aspectj.lang.enums.BusinessType;
import com.yserp.framework.web.controller.BaseController;
import com.yserp.framework.web.domain.AjaxResult;
import com.yserp.framework.web.page.TableDataInfo;
import com.yserp.project.erp.common.enums.OrderEnums;
import com.yserp.project.erp.order.saleorder.domain.ErpSaleOrder;
import com.yserp.project.erp.order.saleorder.domain.ErpSaleOrderDto;
import com.yserp.project.erp.order.saleorder.domain.ErpSaleOrderVo;
import com.yserp.project.erp.order.saleorder.service.IErpSaleOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 销售订单Controller
 *
 * @author yserp
 * @date 2022-07-07
 */
@Controller
@RequestMapping("/order/saleorder")
public class ErpSaleOrderController extends BaseController {
    private String prefix = "erp/order/saleorder";

    @Autowired
    private IErpSaleOrderService erpSaleOrderService;

    @RequiresPermissions("order:saleorder:view")
    @GetMapping()
    public String order() {
        return prefix + "/order";
    }

    /**
     * 查询销售订单列表
     */
    @RequiresPermissions("order:saleorder:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ErpSaleOrder erpSaleOrder) {
        startPage();
        List<ErpSaleOrder> list = erpSaleOrderService.selectErpSaleOrderList(erpSaleOrder);
        return getDataTable(list);
    }

    /**
     * 导出销售订单列表
     */
    @RequiresPermissions("order:saleorder:export")
    @Log(title = "销售订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ErpSaleOrder erpSaleOrder) {
        List<ErpSaleOrder> list = erpSaleOrderService.selectErpSaleOrderList(erpSaleOrder);
        ExcelUtil<ErpSaleOrder> util = new ExcelUtil<ErpSaleOrder>(ErpSaleOrder.class);
        return util.exportExcel(list, "销售订单数据");
    }

    /**
     * 获取单号
     */
    @GetMapping("/getOrderNo")
    @ResponseBody
    public AjaxResult getOrderNo() {
        return AjaxResult.success("",OrderEnums.getCacheOrderNo(OrderEnums.SALE_ORDER.getPref()));
    }

    /**
     * 新增销售订单
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("saleOrderNo", OrderEnums.getCacheOrderNo(OrderEnums.SALE_ORDER.getPref()));
        return prefix + "/add";
    }

    /**
     * 新增保存销售订单
     */
    @RequiresPermissions("order:saleorder:add")
    @Log(title = "销售订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ErpSaleOrderDto erpSaleOrderDto) {
        return toAjax(erpSaleOrderService.insertErpSaleOrder(erpSaleOrderDto));
    }

    /**
     * 修改销售订单
     */
    @RequiresPermissions("order:saleorder:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ErpSaleOrderVo erpSaleOrderVo = erpSaleOrderService.selectErpSaleOrderById(id);
        mmap.put("erpSaleOrderVo", erpSaleOrderVo);
        return prefix + "/edit";
    }

    /**
     * 修改保存销售订单
     */
    @RequiresPermissions("order:saleorder:edit")
    @Log(title = "销售订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ErpSaleOrderDto erpSaleOrderDto) {
        return toAjax(erpSaleOrderService.updateErpSaleOrder(erpSaleOrderDto));
    }

    /**
     * 删除销售订单
     */
    @RequiresPermissions("order:saleorder:remove")
    @Log(title = "销售订单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(erpSaleOrderService.deleteErpSaleOrderByIds(ids));
    }

    /**
     * 审核销售订单
     */
    @RequiresPermissions("order:saleorder:audit")
    @Log(title = "销售订单", businessType = BusinessType.AUDIT)
    @PostMapping("/audit")
    @ResponseBody
    public AjaxResult audit(String ids) {
        return toAjax(erpSaleOrderService.auditErpSaleOrderByIds(ids));
    }


    /**
     * 选择销售订单
     */
    @GetMapping("/selectSaleOrder")
    public String selectSaleOrder() {
        return prefix + "/selectSaleOrder";
    }

    /**
     * 查询销售订单管理列表(选择页面)
     */
    @PostMapping("/selectList")
    @ResponseBody
    public TableDataInfo selectList(ErpSaleOrder erpSaleOrder) {
        startPage();
        List<ErpSaleOrder> list = erpSaleOrderService.selectErpSaleOrderList(erpSaleOrder);
        return getDataTable(list);
    }

    /**
     * 查询销售订单管理列表(选择页面)
     */
    @PostMapping("/ajaxList")
    @ResponseBody
    public AjaxResult ajaxList(ErpSaleOrder erpSaleOrder) {
        List<ErpSaleOrder> list = erpSaleOrderService.selectErpSaleOrderList(erpSaleOrder);
        return AjaxResult.success(list);
    }
}
